package com.burpee.web;

import static com.burpee.test.burpeeDaoTest.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.burpee.service.GroupsService;
import com.burpee.service.HealthpointService;
import com.burpee.service.MemberService;
import com.burpee.ui.Groups;
import com.burpee.ui.Healthpoint;
import com.burpee.ui.Member;
@Controller
@RequestMapping("/member.spring")
public class MemberController {
	@Autowired
	MemberService memberService;
	//	@Autowired
	//	CoordinateService coordinateService;
	//	@Autowired
	//	AuthorityService authorityService;
	@Autowired
	HealthpointService healthpointService;
	
	@Autowired
	GroupsService groupsService;
	//	@Autowired
	//	MessageService messageService;
	@RequestMapping(params="action=login")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, Member member) {

//		boolean login = memberService.login(member);
		
//		boolean result;
//		
//		Member member2 = memberService.view(member.getMemberId());
//		
//		ModelAndView modelAndView = new ModelAndView();
//		
//		JSONObject jsonObject = new JSONObject();
//
//		try {
//			if (member2 != null) {
//				
//				if(member.getMemberPassword().equals(member2.getMemberPassword())){
//					request.getSession().setAttribute("login", member);
//					modelAndView.setViewName("redirect:main.spring");
//					return modelAndView;
//				}
//				else{
//					result=false;
//					jsonObject.put("result", result);
//				}
//			}
//			else {
//				result=true;
//				jsonObject.put("result", result);
//			}
//			
//			PrintWriter out = response.getWriter();
//			
//			out.print(jsonObject.toString());
//		} catch (JSONException e) {
//			e.printStackTrace();
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//		
//		return null;
		
		int result;
		
		Member member2 = memberService.view(member.getMemberId());
		
		ModelAndView modelAndView = new ModelAndView();
		
		JSONObject jsonObject = new JSONObject();

		try {
			if (member2 != null) {
				
				if(member.getMemberPassword().equals(member2.getMemberPassword())){
					result = 1;
					request.getSession().setAttribute("login", member2);
					jsonObject.put("result", result);
				}
				else{
					result=3;
					jsonObject.put("result", result);
				}
			}
			else {
				result=2;
				jsonObject.put("result", result);
			}
			
			PrintWriter out = response.getWriter();
			
			out.print(jsonObject.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return null;

	}

	@RequestMapping(params = "action=logout")
	public ModelAndView logout(HttpServletRequest request) {

		request.getSession().removeAttribute("login");

		return new ModelAndView("redirect:main.spring");
	}

	@RequestMapping(params = "action=preadd")
	public ModelAndView preadd(HttpServletRequest request, HttpServletResponse response, Member member){

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("member/memberAdd");

		return modelAndView;

	}

	@RequestMapping(params = "action=memberadd")
	public ModelAndView memberadd(HttpServletRequest request, HttpServletResponse response, Member member, String Birthday){
		String password = request.getParameter("memberPassword1");
		Date date = new Date(System.currentTimeMillis());
		
		member.setInputDate(date);
		member.setUpdateDate(date);
		member.setMemberBirthday(Date.valueOf(Birthday));
		
		if(member.getMemberPassword().equals(password)){
			memberService.memberadd(member);
		} else {
			System.out.println("경고창을 띄워줍시다");
		}
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:main.spring");

		return modelAndView;

	}

	@RequestMapping(params = "action=premodify")
	public ModelAndView premodify(HttpServletRequest request, HttpServletResponse response, Member member){
		
		member = memberService.view(member.getMemberId());

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("member", member);
		modelAndView.setViewName("member/memberModify");

		return modelAndView;

	}

	@RequestMapping(params = "action=modify")
	public ModelAndView modify(HttpServletRequest request, HttpServletResponse response, Member member, String Birthday){

		String password = request.getParameter("memberPassword1");
		Date date = new Date(System.currentTimeMillis());
		
		member.setUpdateDate(date);
		member.setMemberBirthday(Date.valueOf(Birthday));
		if(member.getMemberPassword().equals(password)){
			memberService.membermodify(member);
		}
		else{
			System.out.println("경고창을 띄워줍시다");
		}
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("redirect:main.spring");

		return modelAndView;

	}

	@RequestMapping(params = "action=mypage")
	public ModelAndView mypage(HttpServletRequest request, HttpServletResponse response, Member member){

		Groups groups;
		
		member = memberService.view(member.getMemberId());
		
		groups = groupsService.memberview(member.getMemberId());
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("groups", groups);
		modelAndView.addObject("member", member);
		modelAndView.setViewName("member/mypage");

		return modelAndView;

	}
	
	@RequestMapping(params = "action=healthCheckUp")
	public ModelAndView healthCheckUp(HttpServletRequest request, HttpServletResponse response, Member member){

		List<Healthpoint> healthpoint = healthpointService.list(member.getMemberId());
		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("member", healthpoint);
		modelAndView.setViewName("member/healthCheckUp");

		return modelAndView;

	}

	@RequestMapping(params = "action=list")
	public ModelAndView privateInformation(HttpServletRequest request, HttpServletResponse response, Member member){

		logger.error("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("healthWritteBook/privateInformation");

		return modelAndView;

	}
	
	@RequestMapping(params = "action=memberIdSearch")
	public ModelAndView memberIdSearch(HttpServletRequest request, HttpServletResponse response){

		
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("member/memberIdSearch");

		return modelAndView;

	}


}
